package br.inatel.ec203.projeto.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;

import br.inatel.ec203.projeto.model.Bjt;
import br.inatel.ec203.projeto.model.Capacitor;
import br.inatel.ec203.projeto.model.Ci;
import br.inatel.ec203.projeto.model.Item;
import br.inatel.ec203.projeto.model.Fet;
import br.inatel.ec203.projeto.model.OutrosComponentes;
import br.inatel.ec203.projeto.model.Resistor;
import br.inatel.ec203.projeto.model.Transistor;

public class EditFrame extends JFrame{

	private static final long serialVersionUID = -6935449419928921578L;
	
	private EditFrameListeners editFrameListener;
	
	private Item component;
	
	private ComponentCommonPanel componentCommonPanel;
	private ResistorSpecificPanel resistorSpecificPanel;
	private CapacitorSpecificPanel capacitorSpecificPanel;
	private CiSpecificPanel ciSpecificPanel;
	private OtherComponentSpecificPanel otherComponentSpecificPanel;
	private TransistorSpecificPanel transistorSpecificPanel;
	private OkCancelButtonsPanel okCancelButtonsPanel;
	
	private GridBagConstraints componentCommonPanelConstraints;
	private GridBagConstraints resistorSpecificPanelConstraints;
	private GridBagConstraints capacitorSpecificPanelConstraints;
	private GridBagConstraints ciSpecificPanelConstraints;
	private GridBagConstraints otherComponentSpecificPanelConstraints;
	private GridBagConstraints transistorSpecificPanelConstraints;
	private GridBagConstraints okCancelButtonsPanelConstraints;

	public EditFrame(Item componente) {
		super ("Editar Componente");
		
		this.component = componente;
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		initialize();
		setSize(new Dimension(600,300));
		setResizable(false);
		setLocationRelativeTo(null);
	}

	private void initialize() {
		setLayout(new GridBagLayout());
		add(getComponentCommonPanel(), getComponentCommonPanelConstraints());
		add(getOkCancelButtonsPanel(), getOkCancelButtonsPanelConstraints());
	}

	private ComponentCommonPanel getComponentCommonPanel() {
		if (componentCommonPanel == null) {
			componentCommonPanel = new ComponentCommonPanel();			
		}
		return componentCommonPanel;
	}

	private ResistorSpecificPanel getResistorSpecificPanel() {
		if (resistorSpecificPanel == null) {
			resistorSpecificPanel = new ResistorSpecificPanel();
		}
		return resistorSpecificPanel;
	}

	private CapacitorSpecificPanel getCapacitorSpecificPanel() {
		if (capacitorSpecificPanel == null) {
			capacitorSpecificPanel = new CapacitorSpecificPanel();
		}
		return capacitorSpecificPanel;
	}
	
	public CiSpecificPanel getCiSpecificPanel() {
		if (ciSpecificPanel == null) {
			ciSpecificPanel = new CiSpecificPanel();
		}
		return ciSpecificPanel;
	}
	
	public OtherComponentSpecificPanel getOtherComponentSpecificPanel() {
		if (otherComponentSpecificPanel == null) {
			otherComponentSpecificPanel = new OtherComponentSpecificPanel();
		}
		return otherComponentSpecificPanel;
	}
	
	public TransistorSpecificPanel getTransistorSpecificPanel() {
		if (transistorSpecificPanel == null) {
			transistorSpecificPanel = new TransistorSpecificPanel();
		}
		return transistorSpecificPanel;
	}

	private OkCancelButtonsPanel getOkCancelButtonsPanel() {
		if (okCancelButtonsPanel == null) {
			okCancelButtonsPanel = new OkCancelButtonsPanel();
			okCancelButtonsPanel.addOkCancelButtonsListeners(new OkCancelButtonsListeners() {
				
				@Override
				public void okPerformed() {
					okClicked();			
				}
				
				@Override
				public void cancelPerformed() {
					dispose();					
				}
			});
		}
		return okCancelButtonsPanel;
	}

	protected void okClicked() {
		component.setDescricao(getComponentCommonPanel().getDescriptionTextField().getText());
		component.setValor(getComponentCommonPanel().getValueTextField().getText());
		component.setPartNumber(getComponentCommonPanel().getPartNumberTextField().getText());
		component.setEncapsulamento(getComponentCommonPanel().getPackageTextField().getText());
		component.setLocalizacao(getComponentCommonPanel().getLocalizationTextField().getText());
		component.setQuantidade(Integer.parseInt(getComponentCommonPanel().getAmountTextField().getText()));
		if (component instanceof Resistor) {
			Resistor resistor = (Resistor) component;
			resistor.setMaterial(getResistorSpecificPanel().getMaterialTextField().getText());
		} else if (component instanceof Capacitor) {
			Capacitor capacitor = (Capacitor) component;
			capacitor.setMaterial(getCapacitorSpecificPanel().getMaterialTextField().getText());
			capacitor.setVidaUtil(getCapacitorSpecificPanel().getVidaUtilTextField().getText());
		} else if (component instanceof Ci) {
			Ci ci = (Ci) component;
			ci.setFuncionalidade(getCiSpecificPanel().getFunctionTextField().getText());
			ci.setInformacoes(getCiSpecificPanel().getInformationTextField().getText());
		} else if (component instanceof OutrosComponentes) {
			OutrosComponentes outrosComponentes = (OutrosComponentes) component;
			outrosComponentes.setTipo(getOtherComponentSpecificPanel().getKindTextField().getText());
			outrosComponentes.setInformacoes(getOtherComponentSpecificPanel().getInformationTextField().getText());
		} else if (component instanceof Transistor) {
			int selectedIndex = getTransistorSpecificPanel().getChannelComboBox().getSelectedIndex();
			String channel = null;
			if (selectedIndex == 1) {
				channel = "N";
			} else if (selectedIndex == 2) {
				channel = "P";
			}
			if (component instanceof Fet) {
				Fet fet = (Fet) component;
				fet.setVds(getTransistorSpecificPanel().getVdsTextField().getText());
				fet.setVgs(getTransistorSpecificPanel().getVgsTextField().getText());
				fet.setId(getTransistorSpecificPanel().getIdTextField().getText());
				fet.setRdsOn(getTransistorSpecificPanel().getRdsOnTextField().getText());
				fet.setCanal(channel);
			} else if (component instanceof Bjt) {
				Bjt bjt = (Bjt) component;
				bjt.setVce(getTransistorSpecificPanel().getVceTextField().getText());
				bjt.setIc(getTransistorSpecificPanel().getIcTextField().getText());
				bjt.setJuncao(channel);
			}
		}
		editFrameListener.internalFrameClosed(null);
		dispose();
	}

	private GridBagConstraints getComponentCommonPanelConstraints() {
		if (componentCommonPanelConstraints == null) {
			componentCommonPanelConstraints = new GridBagConstraints();
			componentCommonPanelConstraints.gridx = 0;
			componentCommonPanelConstraints.gridy = 0;
			componentCommonPanelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return componentCommonPanelConstraints;
	}

	private GridBagConstraints getResistorSpecificPanelConstraints() {
		if (resistorSpecificPanelConstraints == null) {
			resistorSpecificPanelConstraints = new GridBagConstraints();
			resistorSpecificPanelConstraints.gridx = 0;
			resistorSpecificPanelConstraints.gridy = 1;
			resistorSpecificPanelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return resistorSpecificPanelConstraints;
	}

	private GridBagConstraints getCapacitorSpecificPanelConstraints() {
		if (capacitorSpecificPanelConstraints == null) {
			capacitorSpecificPanelConstraints = new GridBagConstraints();
			capacitorSpecificPanelConstraints.gridx = 0;
			capacitorSpecificPanelConstraints.gridy = 1;
			capacitorSpecificPanelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return capacitorSpecificPanelConstraints;
	}
	
	public GridBagConstraints getCiSpecificPanelConstraints() {
		if (ciSpecificPanelConstraints == null) {
			ciSpecificPanelConstraints = new GridBagConstraints();
			ciSpecificPanelConstraints.gridx = 0;
			ciSpecificPanelConstraints.gridy = 1;
			ciSpecificPanelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return ciSpecificPanelConstraints;
	}
	
	public GridBagConstraints getOtherComponentSpecificPanelConstraints() {
		if (otherComponentSpecificPanelConstraints == null) {
			otherComponentSpecificPanelConstraints = new GridBagConstraints();
			otherComponentSpecificPanelConstraints.gridx = 0;
			otherComponentSpecificPanelConstraints.gridy = 1;
			otherComponentSpecificPanelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return otherComponentSpecificPanelConstraints;
	}
	
	public GridBagConstraints getTransistorSpecificPanelConstraints() {
		if (transistorSpecificPanelConstraints == null) {
			transistorSpecificPanelConstraints = new GridBagConstraints();
			transistorSpecificPanelConstraints.gridx = 0;
			transistorSpecificPanelConstraints.gridy = 1;
			transistorSpecificPanelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return transistorSpecificPanelConstraints;
	}

	public GridBagConstraints getOkCancelButtonsPanelConstraints() {
		if (okCancelButtonsPanelConstraints == null) {
			okCancelButtonsPanelConstraints = new GridBagConstraints();
			okCancelButtonsPanelConstraints.gridx = 0;
			okCancelButtonsPanelConstraints.gridy = 2;
			okCancelButtonsPanelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return okCancelButtonsPanelConstraints;
	}
	
	public Item getComponent() {		
		return component;
	}
	
	public void setTextFields() {
		getComponentCommonPanel().getDescriptionTextField().setText(getComponent().getDescricao());
		getComponentCommonPanel().getValueTextField().setText(getComponent().getValor());
		getComponentCommonPanel().getPartNumberTextField().setText(getComponent().getPartNumber());
		getComponentCommonPanel().getPackageTextField().setText(getComponent().getEncapsulamento());
		getComponentCommonPanel().getLocalizationTextField().setText(getComponent().getLocalizacao());
		getComponentCommonPanel().getAmountTextField().setText(Integer.toString(getComponent().getQuantidade()));		
		if (getComponent() instanceof Resistor) {
			remove(getCapacitorSpecificPanel());
			remove(getCiSpecificPanel());
			remove(getOtherComponentSpecificPanel());
			add(getResistorSpecificPanel(), getResistorSpecificPanelConstraints());
			Resistor resistor = (Resistor) getComponent();
			getResistorSpecificPanel().getMaterialTextField().setText(resistor.getMaterial());
		}
		else if (getComponent() instanceof Capacitor) {
			remove(getResistorSpecificPanel());
			remove(getCiSpecificPanel());
			remove(getOtherComponentSpecificPanel());
			add(getCapacitorSpecificPanel(), getCapacitorSpecificPanelConstraints());
			Capacitor capacitor = (Capacitor) getComponent();
			getCapacitorSpecificPanel().getMaterialTextField().setText(capacitor.getMaterial());
			getCapacitorSpecificPanel().getVidaUtilTextField().setText(capacitor.getVidaUtil());
		}
		else if (getComponent() instanceof Ci) {
			remove(getResistorSpecificPanel());
			remove(getCapacitorSpecificPanel());
			remove(getOtherComponentSpecificPanel());
			add(getCiSpecificPanel(), getCiSpecificPanelConstraints());
			Ci ci = (Ci) getComponent();
			getCiSpecificPanel().getFunctionTextField().setText(ci.getFuncionalidade());
			getCiSpecificPanel().getInformationTextField().setText(ci.getInformacoes());
		}
		else if (getComponent() instanceof OutrosComponentes) {
			remove(getResistorSpecificPanel());
			remove(getCapacitorSpecificPanel());
			remove(getCiSpecificPanel());
			add(getOtherComponentSpecificPanel(), getOtherComponentSpecificPanelConstraints());
			OutrosComponentes outroComponente = (OutrosComponentes) getComponent();
			getOtherComponentSpecificPanel().getKindTextField().setText(outroComponente.getTipo());
			getOtherComponentSpecificPanel().getInformationTextField().setText(outroComponente.getInformacoes());
		}
		else if (getComponent() instanceof Transistor) {
			remove(getResistorSpecificPanel());
			remove(getCapacitorSpecificPanel());
			remove(getCiSpecificPanel());
			remove(getOtherComponentSpecificPanel());
			// Adaptar painel de Especifico de transistor para editar
			getTransistorSpecificPanel().remove(transistorSpecificPanel.getFetRadioButton());
			getTransistorSpecificPanel().remove(transistorSpecificPanel.getBjtRadioButton());	
			
			if (getComponent() instanceof Fet) {
				add(getTransistorSpecificPanel(), getTransistorSpecificPanelConstraints());
				Fet fet = (Fet) getComponent();
				
				// Adaptar painel de Especifico de transistor para editar					
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getVdsLabel(), getTransistorSpecificPanel().getVdsLabelConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getVdsTextField(), getTransistorSpecificPanel().getVdsTextFieldConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getVgsLabel(), getTransistorSpecificPanel().getVgsLabelConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getVgsTextField(), getTransistorSpecificPanel().getVgsTextFieldConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getIdLabel(), getTransistorSpecificPanel().getIdLabelConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getIdTextField(), getTransistorSpecificPanel().getIdTextFieldConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getRdsOnLabel(), getTransistorSpecificPanel().getRdsOnLabelConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getRdsOnTextField(), getTransistorSpecificPanel().getRdsOnTextFieldConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getChannelLabel(), getTransistorSpecificPanel().getChannelLabelConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getChannelComboBox(), getTransistorSpecificPanel().getChannelComboBoxConstraints());
				
				getTransistorSpecificPanel().getVdsTextField().setText(fet.getVds());
				getTransistorSpecificPanel().getVgsTextField().setText(fet.getVgs());
				getTransistorSpecificPanel().getIdTextField().setText(fet.getId());
				getTransistorSpecificPanel().getRdsOnTextField().setText(fet.getRdsOn());
				int selectedIndex = 0;
				String channel = fet.getCanal();
				if (channel.equals("N")) {
					selectedIndex = 1;
				} else if (channel.equals("P")) {
					selectedIndex = 2;
				}
				getTransistorSpecificPanel().getChannelComboBox().setSelectedIndex(selectedIndex);
			} else if (getComponent() instanceof Bjt) {
				add(getTransistorSpecificPanel(), getTransistorSpecificPanelConstraints());
				Bjt bjt = (Bjt) getComponent();
				
				// Adaptar painel de Especifico de transistor para editar						
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getVceLabel(), getTransistorSpecificPanel().getVceLabelConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getVceTextField(), getTransistorSpecificPanel().getVceTextFieldConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getIcLabel(), getTransistorSpecificPanel().getIcLabelConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getIcTextField(), getTransistorSpecificPanel().getIcTextFieldConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getJunctionLabel(), getTransistorSpecificPanel().getJunctionLabelConstraints());
				getTransistorSpecificPanel().add(getTransistorSpecificPanel().getChannelComboBox(), getTransistorSpecificPanel().getChannelComboBoxConstraints());
				
				getTransistorSpecificPanel().getVceTextField().setText(bjt.getVce());
				getTransistorSpecificPanel().getIcTextField().setText(bjt.getIc());
				int selectedIndex = 0;
				String juncao = bjt.getJuncao();
				if (juncao.equals("N")) {
					selectedIndex = 1;
				} else if (juncao.equals("P")) {
					selectedIndex = 2;
				}
				getTransistorSpecificPanel().getChannelComboBox().setSelectedIndex(selectedIndex);
			}			
		}
	}
	
	public void addEditFrameListener(EditFrameListeners mainFrameListener) {
		this.editFrameListener = mainFrameListener;
	}

}












